package org.lq.us.web;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.lq.us.commons.Page;
import org.lq.us.entity.Role;
import org.lq.us.service.RoleService;
import org.lq.us.service.impl.RoleServiceImpl;

import com.google.gson.Gson;

/**
 * Servlet implementation class RoleServlet
 */
@WebServlet("/RoleServlet")
public class RoleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RoleServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    RoleService roleService = new RoleServiceImpl();
    Gson gson = new Gson();
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String method  =request.getParameter("method");
		switch (method) {
		case "findAll":
			//当前页数
			int currentPageNum= Integer.parseInt(request.getParameter("page"));
			int pageSize = Integer.parseInt(request.getParameter("limit"));
			//查询名称
			String name = request.getParameter("name");
			//创建分页对象
			Page<Role> page = new Page<Role>(currentPageNum, pageSize, roleService.getCount(name));
			//调用分页查询
			roleService.findPage(page, name);
			List<Role> list = page.getList();
			
			//{"code":0,"msg":"","count":1000,"data":[]}
			
//			response.getWriter().print(gson.toJson(list));
			
			Map<String,Object> map = new HashMap<>();
			map.put("code", 0);
			map.put("msg","");
			map.put("count",page.getTotalCount());
			map.put("data", list);
			response.getWriter().write(gson.toJson(map));
			break;
		case "add":
			Map<String,String[]> initParam= request.getParameterMap();
			Role role = new Role();
			try {
				BeanUtils.populate(role, initParam);
			} catch (IllegalAccessException | InvocationTargetException e) {
				e.printStackTrace();
			}
			if(roleService.save(role)>0) {
				response.getWriter().print("ok");
			}else {
				response.getWriter().print("no");
			}
			break;
		case "del":
			String id= request.getParameter("id");
			int num =roleService.deltet(Integer.parseInt(id));
			if(num>0) {
				response.getWriter().print("ok");
			}else {
				response.getWriter().print("no");
			}
			break;
		case "update":
			Map<String,String[]> initParam2= request.getParameterMap();
			Role role2 = new Role();
			try {
				BeanUtils.populate(role2, initParam2);
			} catch (IllegalAccessException | InvocationTargetException e) {
				e.printStackTrace();
			}
			if(roleService.update(role2)>0) {
				response.getWriter().print("ok");
			}else {
				response.getWriter().print("no");
			}
			
			break;
			
		case "all":
			Page<Role> page3 = new Page<Role>(1, roleService.getCount(""), roleService.getCount(""));
			roleService.findPage(page3, "");
			response.getWriter().print(gson.toJson(page3.getList()));
			break;
		case "pie":
			response.getWriter().print(roleService.getPieData());
			break;
		default:
			break;
		}
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
